//
//  NSMutableAttributedString+OBMoment.m
//  WeChat
//
//  Created by 黄柳姣 on 2018/5/3.
//  Copyright © 2018年 obally. All rights reserved.
// 

#import "NSMutableAttributedString+OBMoment.h"
#import "OBMomentHelper.h"

@implementation NSMutableAttributedString (OBMoment)
- (void)ob_regexContentWithWithEmojiImageFontSize:(CGFloat)fontSize
{
    ///高亮背景
    YYTextBorder *border = [YYTextBorder new];
    border.cornerRadius = 0;
    border.insets = UIEdgeInsetsMake(0, -1, 0, -1);
    border.fillColor = OBMomentTextHighlightBackgroundColor;
    
    /// 匹配链接
    NSArray<NSTextCheckingResult *> *linkUrlResults = [[OBMomentHelper regexLinkUrl] matchesInString:self.string options:kNilOptions range:self.rangeOfAll];
    for (NSTextCheckingResult *link in linkUrlResults) {
        if (link.range.location == NSNotFound && link.range.length <= 1) {
            continue;
        }
        if ([self attribute:YYTextHighlightAttributeName atIndex:link.range.location] == nil) {
            [self setColor:OBMomentContentUrlTextColor range:link.range];
            ///点击高亮
            YYTextHighlight *highLight = [YYTextHighlight new];
            highLight.userInfo = @{OBMomentLinkUrlKey:[self.string substringWithRange:link.range]};
            [highLight setBackgroundBorder:border];
            [self setTextHighlight:highLight range:link.range];
        }
    }
    
    /// 匹配电话号码
    NSArray<NSTextCheckingResult *> *phoneResults = [[OBMomentHelper regexPhoneNumber] matchesInString:self.string options:kNilOptions range:self.string.rangeOfAll];
    
    for (NSTextCheckingResult *phone in phoneResults) {
        if (phone.range.location == NSNotFound && phone.range.length <= 1) continue;
        if ([self attribute:YYTextHighlightAttributeName atIndex:phone.range.location] == nil) {
            [self setColor:OBMomentContentUrlTextColor range:phone.range];
            /// 匹配userInfo
            /// 点击高亮
            YYTextHighlight *highlight = [YYTextHighlight new];
            // 将用户数据带出去
            highlight.userInfo = @{OBMomentPhoneNumberKey:[self.string substringWithRange:phone.range]};
            [highlight setBackgroundBorder:border];
            [self setTextHighlight:highlight range:phone.range];
        }
    }
}
@end
